package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.entity.vo.MassifVo;
import com.lsyw.intergrate.service.MassifGridService;
import com.lsyw.intergrate.service.MassifHoleService;
import com.lsyw.intergrate.service.MassifService;
import com.lsyw.intergrate.service.MassifUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地块 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-27
 */
@Api(tags = "地块")
@RestController
@RequestMapping("/massif")
public class MassifController {


    @Autowired
    private MassifService massifService;
    @Autowired
    private MassifGridService massifGridService;
    @Autowired
    private MassifUserService massifUserService;
    @Autowired
    private MassifHoleService massifHoleService;
    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    @ApiOperation(value = "通过id查询", notes = "通过id查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result getById(String id) {
        Massif massif = massifService.getById(id);
        //网格
        LambdaQueryWrapper<MassifGrid> gridLambdaQueryWrapper = new QueryWrapper<MassifGrid>().lambda();
        gridLambdaQueryWrapper.eq(MassifGrid::getDelFlag,0).eq(MassifGrid::getMassifId,massif.getId());
        List<MassifGrid> massifGridList = this.massifGridService.list(gridLambdaQueryWrapper);

        //人员
        LambdaQueryWrapper<MassifUser> userLambdaQueryWrapper = new QueryWrapper<MassifUser>().lambda();
        userLambdaQueryWrapper.eq(MassifUser::getDelFlag,0).eq(MassifUser::getMassifId,massif.getId());
        List<MassifUser> userList = this.massifUserService.list(userLambdaQueryWrapper);

        //基坑
        LambdaQueryWrapper<MassifHole> holeLambdaQueryWrapper = new QueryWrapper<MassifHole>().lambda();
        holeLambdaQueryWrapper.eq(MassifHole::getDelFlag,0).eq(MassifHole::getMassifId,massif.getId());
        List<MassifHole> holeList = this.massifHoleService.list(holeLambdaQueryWrapper);

        MassifVo massifVo = new MassifVo();
        massifVo.setMassif(massif);
        massifVo.setMassifGrids(massifGridList);
        massifVo.setMassifUsers(userList);
        massifVo.setMassifHoles(holeList);
        return Result.successResult(massifVo);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST")
    public Result save(@RequestBody Massif massif) {
        massif.setDelFlag(0);
        massifService.save(massif);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id")
    @ApiOperation(value = "通过id删除", notes = "通过id删除", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result delete(String id) {
        Massif massif = massifService.getById(Long.parseLong(id));
        massif.setDelFlag(1);
        this.massifService.updateById(massif);
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST")
    public Result updateById(@RequestBody Massif massif) {
        massif.setDelFlag(0);
        massif.setGmtModified(new Date());
        massifService.updateById(massif);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询列表", httpMethod = "POST")
    public Result list(@RequestBody MassifReqVo massif) {
        final LambdaQueryWrapper<Massif> lambda = new QueryWrapper<Massif>().lambda();
        this.buildCondition(lambda, massif);
        return Result.successResult(massifService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "POST")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<Massif> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        Map<String, Object> condition = pageInfo.getCondition();
        final LambdaQueryWrapper<Massif> lambda = new QueryWrapper<Massif>().lambda();
        lambda.eq(Massif::getDelFlag,0);
        if (condition.get("name") != null && !StringUtils.isEmpty(condition.get("name").toString())) {
            lambda.like(Massif::getName, condition.get("name"));
        }
        IPage<Massif> userPage = this.massifService.page(page, lambda);
        return Result.successResult(userPage);
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<Massif> lambda, MassifReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(Massif::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(Massif::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getPollutionLevel())) {
            lambda.eq(Massif::getPollutionLevel, param.getPollutionLevel());
        }
        if (!StringUtils.isEmpty(param.getCode())) {
            lambda.eq(Massif::getCode, param.getCode());
        }
        if (!StringUtils.isEmpty(param.getSort())) {
            lambda.eq(Massif::getSort, param.getSort());
        }

        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(Massif::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(Massif::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(Massif::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(Massif::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(Massif::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(Massif::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(Massif::getDelFlag, param.getDelFlag());
        }
        lambda.orderBy(true, false, Massif::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class MassifReqVo extends Massif {
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
